package com.apps.ftapp.activity;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.SeekBar;

import com.apps.ftapp.R;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.ScreenUtils;
import com.apps.ftapp.util.StatusBarUtils;
import com.duobeiyun.bean.ChatBean;
import com.duobeiyun.callback.LiveMessageCallback;
import com.duobeiyun.callback.OpenglSupport;
import com.duobeiyun.callback.VideoCallback;
import com.duobeiyun.opengles.GLFrameSurface;
import com.duobeiyun.player.LivePlayer;
import com.duobeiyun.type.RoleType;
import com.duobeiyun.widget.LivePlayerView;

import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import es.dmoral.toasty.Toasty;

public class DBOnlineActivity extends BaseActivity implements LiveMessageCallback, VideoCallback {

    @BindView(R.id.player)
    LivePlayerView playerView;
    @BindView(R.id.sdsdf)
    RelativeLayout sdsdf;
    @BindView(R.id.mSeekBar)
    SeekBar mSeekBar;
    @BindView(R.id.dbs_teacher)
    GLFrameSurface mTeachersurface;


    private LivePlayer player;

    private Handler handler = null;

    private String roomId;
    private String title;
    private String uid;
    private String nickname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dbonline);
        ButterKnife.bind(this);


        quanping.setVisibility(View.VISIBLE);
        //创建属于主线程的handler
        handler = new Handler();
        //新页面接收数据
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        roomId = bundle.getString("roomId");
        title = bundle.getString("title");
        uid = bundle.getString("uid");
        nickname = bundle.getString("nickname");

        player = new LivePlayer(getApplicationContext(), playerView);

        StatusBarUtils.StatusBarLightMode(this);

        setTitle(title);

        quanping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                L.i("main", "切换成横屏");
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                quanping.setVisibility(View.GONE);
                RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) sdsdf.getLayoutParams();
                linearParams.height = ScreenUtils.getScreenHeight(getApplicationContext());// 控件的高强制设成20
                sdsdf.setLayoutParams(linearParams);
                toolbar.setVisibility(View.GONE);
            }
        });

        initPlayerAndSetCallback();


    }


    @Override
    protected void onStart() {
        super.onStart();
        if (player != null)
            player.start();
    }

    @Override
    protected void onPause() {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        super.onPause();
        if (player != null)
            player.start();
    }

    @Override
    protected void onDestroy() {
        if (player != null)
            player.release();
        super.onDestroy();
    }

    private boolean useOpenGl = false;

    public void initPlayerAndSetCallback() {

        player.setVideoView(null, null);
        player.setUseOpengl(useOpenGl);
        // 设置回调外层自己控制视频的娴熟与隐藏
        player.setVideoCallback(this);
        // 设置老师视频窗口
        player.setSurfaceView(mTeachersurface);
        try {
            player.setTeacherFrameSurface(mTeachersurface);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mTeachersurface.setVisibility(View.GONE);
        //旧方法可以删除不设置
        player.setOpenglSupport(new OpenglSupport() {
            @Override
            public void unSupportUseOpenGL(final String glmessage) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {


                        Toasty.normal(getApplicationContext(), glmessage).show();
                        mTeachersurface.setVisibility(View.GONE);
                        useOpenGl = false;
                        player.setUseOpengl(useOpenGl);
                    }
                });
            }
        });
        player.setAuth(Constant.PID, Constant.APPKEY);
        int role = RoleType.ROLE_TYPE_STUDENT;
        L.i("main", uid + "=======filePath::::" + roomId + "====" + title + "::::" + nickname);
        player.setPlayInfo(uid, roomId, nickname, role, true, this);
    }

    @Override
    public void loading() {

    }

    @Override
    public void handleContent(ChatBean chatBean) {

    }

    @Override
    public void handleContent(ArrayList<ChatBean> arrayList) {

    }

    @Override
    public void handleClearChat() {

    }

    @Override
    public void statusCode(int i, String s) {

    }

    @Override
    public void handleAnnounceMessage(String s) {

    }

    @Override
    public void connected() {

    }

    @Override
    public void connectFail(String s) {

    }

//    @Override
//    public void connectTimeout() {
//
//    }

    @Override
    public void kickoff() {

    }

    @Override
    public void networkNotConnected() {

    }

    @Override
    public void voteStart(int i) {

    }

    @Override
    public void voteEnd() {

    }

    @Override
    public void voteClose() {

    }

    @Override
    public void voteInfo(JSONObject jsonObject) {

    }


    @Override
    public void teacherOnline() {

    }

    @Override
    public void pptMessage(int i, int i1) {

    }

    @Override
    public void onlineUserCount(int i) {

    }

    @Override
    public void showvideo(int role) {
        if (useOpenGl) {
            if (role == RoleType.ROLE_TYPE_TEACHER) {
                mTeachersurface.setVisibility(View.VISIBLE);
            }
        } else {
            if (role == RoleType.ROLE_TYPE_TEACHER) {
                //teacherImg.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void hidenVideo(int role) {
        if (useOpenGl) {
            if (role == RoleType.ROLE_TYPE_TEACHER) {
                mTeachersurface.setVisibility(View.GONE);
            }
        } else {
            if (role == RoleType.ROLE_TYPE_TEACHER) {
                //teacherImg.setVisibility(View.GONE);
            }
        }
    }
}
